package com.spzx.cloud.order.config;

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer;
import org.springframework.cloud.loadbalancer.core.RoundRobinLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

@Configuration
public class CustomLoadBalancerConfiguration {

    /**
     * @param environment: 用于获取环境属性配置, 其中 LoadBalancerClientFactory.PROPERTY_NAME 表示该负载均衡器要应用的服务名称
     * @param loadBalancerClientFactory: 是 Spring Cloud 中用于创建负载均衡器的工厂类,通过 getLazyProvider 方法获取 ServiceInstanceListSupplier 对象,以提供可用的服务列表
     * ServiceInstanceListSupplier: 用于提供 ServiceInstance 列表的接口,可以从 DiscoveryClient 或者其他注册中心中获取可用的服务实例列表
     * @return
     */
    @Bean
    ReactorLoadBalancer<ServiceInstance> randomLoadBalancer(Environment environment, LoadBalancerClientFactory loadBalancerClientFactory) {
        String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
        return new RoundRobinLoadBalancer(loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), name);
    }
}
